package com.itheima.demo;

import com.itheima.domain.Person;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/7/19 13:05
 */
public class Demo_XML_002 {
    public static void main(String[] args) throws Exception {
        //获取解析器 ;
        SAXReader sr = new SAXReader();
        Document document = sr.read(new File("demo_xml\\xml\\Person.xml"));
        //获取根标签 ;
        Element element = document.getRootElement();
        //获取根标签下面的所有子标签;
        List<Element> persons = element.elements("person");

        ArrayList<Person> list = new ArrayList<>();
        //遍历 ;
        for (Element elements : persons) {
            //获取属性值 ;
            Attribute attribute = elements.attribute("id");
            //获取值;
            String id = attribute.getValue();

            //获取子标签下面的标签内容;
            Element nameElement = elements.element("name");
            String name = nameElement.getText();
            Element ageElement = elements.element("age");
            String age = ageElement.getText();
            Element tell = elements.element("tellPhone");
            String tellPhone = tell.getText();
            Person p = new Person(id, name, Integer.parseInt(age), tellPhone);
            list.add(p);

        }
        for (Person person : list) {
            System.out.println(person);
        }
    }
}
